//
//  TagViewController.m
//  PhotoTag
//
//  Created by Andrew on 2018/8/3.
//  Copyright © 2018年 Andrew. All rights reserved.
//

#import "TagViewController.h"
#import "ZZPhoto.h"
#import "UIView+Extension.h"
#import "TagLabelView.h"
#import "LBTagView.h"
#import "YMStringSize.h"
#import "ImageArrModel.h"
#import "SignModel.h"

@interface TagViewController () <UIScrollViewDelegate>

/* 关闭按钮 */
@property (nonatomic, strong) UIButton *closeBtn;
/* 创建scrollview */
@property (nonatomic, strong) UIScrollView *scrollView;
/* 点击添加标签 */
@property (nonatomic, strong) UIButton *addTagBtn;
/* 当前页数 */
@property (nonatomic, assign) NSInteger currentPage;




@end

@implementation TagViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //添加按钮
    [self setupNaviBtn];
    [self setupScrollView];
    [self addTagBtn];
}

- (void)setupScrollView
{
    [self scrollView];
    
    //大图
    for (NSInteger i = 0; i < self.datasource.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.x = SCREENWIDTH * i;
        imageView.y = 0;
        imageView.height = self.scrollView.height;
        imageView.width = SCREENWIDTH;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.userInteractionEnabled = YES;
        imageView.tag = i + 1;
        
        ZZPhoto *photo = self.datasource[i];
        imageView.image = photo.originImage;
        [self.scrollView addSubview:imageView];
    }
    
    //压缩图
    CGFloat imageWH = 50;
    for (NSInteger i = 0; i < self.datasource.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.x = 10 + (imageWH +10) * i;
        imageView.y = 74;
        imageView.width = imageWH;
        imageView.height = imageWH;
        imageView.tag = i;
        
        ZZPhoto *photo = self.datasource[i];
        imageView.image = photo.originImage;
        
        [self.view addSubview:imageView];
    }
    self.scrollView.contentSize = CGSizeMake(SCREENWIDTH * self.datasource.count, 0);
}

#pragma mark - 关闭页面
- (void)setupNaviBtn
{
    //自定义返回
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"返回" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.frame = CGRectMake(0, 0, 15, 40);
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //右边完成按钮
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    rightBtn.backgroundColor = YMColor(29, 160, 115);
    rightBtn.layer.cornerRadius = 5;
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightBtn addTarget:self action:@selector(finishBtnClick) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.frame = CGRectMake(0, 0, 55, 30);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

#pragma mark - 完成
- (void)finishBtnClick
{
    NSMutableArray *imagesource = [NSMutableArray new];
    for (NSInteger i = 0; i < self.scrollView.subviews.count; i++) {
        UIImageView *imageView = [self.scrollView viewWithTag:i + 1];
        if (imageView) {
            ImageArrModel *imageModel = [[ImageArrModel alloc] init];
            NSMutableArray *tagArr = [NSMutableArray new];
            for (NSInteger i = 0; i < imageView.subviews.count; i++) {
                SignModel *model = [[SignModel alloc] init];
                LBTagView *tag = imageView.subviews[i];
                model.x = tag.x;
                model.y = tag.y / self.scrollView.height;
                model.w = tag.width;
                model.h = tag.height;
                model.text = tag.text;
                model.rediret = tag.direction;
                [tagArr addObject:model];
            }
            imageModel.image = imageView.image;
            imageModel.siglableArr = tagArr;
            [imagesource addObject:imageModel];
        }
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary new];
    dic[@"imageSource"] = imagesource;
    [[NSNotificationCenter defaultCenter] postNotificationName:PostImagesNotifi object:self userInfo:dic];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 添加标签
- (void)addTagClick
{
    TagLabelView *tagView = [TagLabelView ym_viewFormXib];
    tagView.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
    [self.view addSubview:tagView];
    WS(ws);
    tagView.tagLabelViewBlock = ^(NSString *text) {
        
        [ws addTagInImage:text];
    };
}

- (void)addTagInImage:(NSString *)text
{
    //获取要添加标签的图片
    UIImageView *imageView = self.scrollView.subviews[self.currentPage];
    
    CGFloat lableW = [YMStringSize ym_sizeWithText:text font:LabelFontRegular(13) maxW:MAXFLOAT].width;
    LBTagView *label = [[LBTagView alloc] init];
    label.width = lableW + 10;
    label.height = 20;
    label.x = imageView.width * 0.5 - label.width * 0.5;
    label.y = imageView.height * 0.5 - label.height * 0.5;
    label.font = LabelFontRegular(13);
    label.text = text;
    [imageView addSubview:label];
    
    //添加移动手势
    UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveAndChangeTagPosition:)];
    [label addGestureRecognizer:panGest];
    
    //点击改变方向
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapChangeDirection:)];
    [label addGestureRecognizer:tap];
}

- (void)moveAndChangeTagPosition:(UIPanGestureRecognizer *)panGest
{
    //拖拽的距离 [距离是一个累加]
    CGPoint trans = [panGest translationInView:panGest.view];
    LBTagView *tagView = (LBTagView *)panGest.view;
    tagView.center = CGPointMake(tagView.center.x + trans.x, tagView.center.y + trans.y);
    //清除 累加 的距离
    [panGest setTranslation:CGPointZero inView:panGest.view];
}

- (void)tapChangeDirection:(UITapGestureRecognizer *)tapGest
{
    LBTagView *tagView = (LBTagView *)tapGest.view;
    if (tagView.direction == LBTagDirectionLeft) {
        tagView.direction = LBTagDirectionRight;
    } else {
        tagView.direction = LBTagDirectionLeft;
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat page = scrollView.contentOffset.x / SCREENWIDTH;
    self.currentPage = page;
}

#pragma mark - 懒加载
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 134, SCREENWIDTH, SCREENHEIGHT - 134 - 80)];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

- (UIButton *)addTagBtn
{
    if (!_addTagBtn) {
        _addTagBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, SCREENHEIGHT - 60, SCREENWIDTH - 20, 40)];
        _addTagBtn.backgroundColor = YMColor(123, 233, 56);
        _addTagBtn.layer.cornerRadius = 5;
        [_addTagBtn setTitle:@"添加标签" forState:UIControlStateNormal];
        [_addTagBtn addTarget:self action:@selector(addTagClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_addTagBtn];
    }
    return _addTagBtn;
}


@end
